<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath%>">
<%@include file="/esayuiStyle.jsp" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
	$(function(){
		$("#permissionTable").treegrid({
			toolbar:"#tb",
			idField:"id",
			treeField:"text",
			animate:true,
			onLoadSuccess : function(){
				$(this).treegrid("collapseAll");
			},
			loadFilter : function(data){
				$.each(data,function(){
					this.state = "closed"
				})
				return data;
			}
		});
	});
	function addPermission(){
		var d = $("<div></div>").appendTo("body");
		d.dialog({
			title : "添加权限",
			iconCls : "icon-add",
			width:500,
			height:400,
			modal:true,
			href : "permission/permissionForm",
			onClose:function(){$(this).dialog("destroy"); },
			buttons:[{
				iconCls:"icon-ok",
				text:"确定",
				handler:function(){
					$("#permissionForm").form("submit",{
						url : "permission/permissionAdd",
						success : function(data){
							d.dialog("close");
							$("#permissionTable").treegrid("reload");
						}
					});
				}
			},{
				iconCls:"icon-cancel",
				text:"取消",
				handler:function(){
					d.dialog("close");
				}
			}]
		});
	}
	
	function deletePermission(){
		var selRows = $("#permissionTable").datagrid("getSelections");
		if(selRows.length == 0){
			$.messager.alert("提示","请选择要删除的数据行！","warning");
			return;
		}
		$.messager.confirm("提示","确定要删除选中的数据吗？",function(r){
			if(r){
				var postData = "";
				$.each(selRows,function(i){
					postData += "id="+this.id;
					if(i < selRows.length -1){
						postData += "&";
					}
				});
				$.post("permission/permissionById",postData,function(data){
					if(data.children.length == 0){
						$.post("permission/permissionDelete",postData,function(data){
							if(data.result == true){
								//4. 删除成功后，刷新表格 reload
								$("#permissionTable").treegrid("reload");
							}
						});
					}else{
						$.messager.alert("提示","该类有数据，不可删除","warning");
					}
				});
			}
		});
	}
	function editPermission(){
		var row = $("#permissionTable").datagrid("getSelected");
		if(row == null){
			return;
		}

		//如果选中了多个，只保留row这个
		$("#permissionTable").datagrid("clearSelections");
		$("#permissionTable").datagrid("selectRecord",row.id);
		
		$.post("permission/permissionById",{id:row.id},function(data){
				var d = $("<div></div>").appendTo("body");
				d.dialog({
					title : "修改权限",
					iconCls : "icon-edit",
					width:500,
					height:400,
					modal:true,
					href : "permission/permissionForm",
					onClose:function(){$(this).dialog("destroy"); },
					onLoad:function(){
						//发送异步请求，查询数据
						$.post("permission/permissionById",{id:row.id},function(data){
							$("#permissionForm").form("load",data);
							//$("#parentPermission").combobox({disabled: true}); //下拉框的状态为禁用
							$("#parentPermission").combobox("select",data.parentPermission);
						});
					},
					buttons:[{
						iconCls:"icon-ok",
						text:"确定",
						handler:function(){
							$("#permissionForm").form("submit",{
								url : "permission/permissionEdit",
								success : function(data){
									d.dialog("close");
									$("#permissionTable").treegrid("reload");
								}
							});
						}
					},{
						iconCls:"icon-cancel",
						text:"取消",
						handler:function(){
							d.dialog("close");
						}
					}]
				});
		});
	}
</script>
<body>

<table id="permissionTable"  title="PermissionList" 
        data-options="url:'permission/permissionAll',fitColumns:true,striped:true,iconCls:'icon-search'">
    <thead>
        <tr>
            <th data-options="field:'text',width:100,sortable:true">权限名称</th>
            <th data-options="field:'type',width:100">类型</th>
            <th data-options="field:'url',width:100">访问地址</th>
            <th data-options="field:'percode',width:100">标识</th>
        </tr>
    </thead>
</table>
<div id="tb">
	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="addPermission();" data-options="iconCls:'icon-add',plain:true">添加权限</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="editPermission();" data-options="iconCls:'icon-edit',plain:true">修改权限</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="deletePermission();" data-options="iconCls:'icon-remove',plain:true">删除权限</a>
</div>
</body>
</html>